function Agency_Code()
{
	var buffer = new StringBuffer();
	buffer.append('<div class="weitab"><div class="cusline1 select ui-border-r" val="1" style="width:50%">我的客户</div><div class="cusline2" val="2" style="width:50%">搜索客户</div></div>');
	buffer.append('<div class="weipanel border-top"></div>');
	buffer.append('<div id="weipanel10" pageindex="0" class="weipanel1" style="height:'+(sheight-21)+'px; top:36px;"><div id="wrapper10"><div id="scroller"><div id="reflash10"></div><div id="info10" style="padding-top:70px;"></div><div id="loading10"><img src="img/loading.gif"/></div></div></div></div>');
	buffer.append('<div class="weipanel2 hidden" style="top:40px;"><div id="find" class="finds2" style="background-color:#fff; position:relative;"><div class="searchinput ui-border-radius"><input id="keyword" class="bordersolid" type="text" value="" onblur="Show_Search();Show_Tips();" onkeyup="Company_Search(this);"/><div id="searchicon" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons" class="searchicons"><img src="img/jings.jpg"/></div></div></div></div>');
	buffer.append('<div class="weicompany hidden">输入11位手机号码后自动开始搜索</div>');
	return buffer.toString();
};

function Init_Agency()
{	
	get("find").addEventListener("tap",function(){
				      				$("#searchicon").hide();
	      							$("#searchicons").show();
	      							$("#keyword").attr("placeholder","输入11位手机号码后自动开始搜索");
	      							if($("#keyword").val().Trim()=="")
	      							{
	      								$(".weicompany").html("");
	      							}
			    					if($("keyword").val()!="")
			    					{
			    						//WorkMate_Search();
			    					}
	});
	mui(".weitab").on("tap","div",function(){
			$(this).addClass("select").siblings().removeClass("select");
			if($(this).attr("val")=="1")
			{
				$(".weipanel1,#divmsg2 .tool").removeClass("hidden");
				$(".weipanel2,.weicompany").addClass("hidden");
				AgencyUser_Search();
			}
			if($(this).attr("val")=="2")
			{
				$(".weipanel2,.weicompany").removeClass("hidden");
				$(".weipanel1,#divmsg2 .tool").addClass("hidden");
			}
	});
};
function Show_Tips()
{
	if($("#keyword").val().Trim().length!=11)
	{
		$(".weicompany").html("输入11位手机号码后自动开始搜索");
	}
};
function AgencyUser_Search(){
	$("#weipanel10").attr("pageindex","0");
	pageindex=1;
	Receive_AgencyUser(1);
};

function AgencyUser_Data(data){
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="weilist border-bottom" id="'+info.ID+'" style="height:110px;"><div class="weicontext" style="width:100%;"><div class="weititle">'+info.Name+'</div><div class="weibottom"><div class="click" style="width:40%;">创建人：'+info.RealName+'</div><div class="createdate" style="width:60%;">手机号：<a>'+info.UserName+'</a></div></div><div class="weibottom"><div class="click" style="width:40%;">成交总额：'+info.Money+'</div><div class="createdate" style="width:60%;">注册时间：'+info.CreateDate.ToTime()+'</div></div><div class="weibottom"><div class="click" style="width:40%;"></div><div class="createdate" style="width:60%;">到期时间：'+info.EndDate.ToTime()+'</div></div></div></div>');
	});
	return buffer.toString();
};

function Receive_AgencyUser(pageindex)
{
	var data = {};
			data.act = "Find_AgencyUser";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE10;
			data.keyword=RUndefined($("#keyword_search").val());
			data.createdate=RUndefined($("#begindate_search").val()) + "～" + RUndefined($("#enddate_search").val());
			data.enddate=RUndefined($("#begindate_expire").val()) + "～" + RUndefined($("#enddate_expire").val());
			data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				if(json.data.comlist.length>0)
				{
					if(pageindex==1)
					{
						$("#info10").html(AgencyUser_Data(json.data.comlist));
					}else{
						$("#info10").append(AgencyUser_Data(json.data.comlist));
					}
					if(json.data.comlist.length!=data.pagesize)
					{
						$("#loading10").html("");
					}else{
						$("#loading10").html('<img src="img/loading.gif"/>');
					}
					setTimeout(function(){
						Init_AgencyCompany("#weipanel10");
						myScroll10.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.comlist.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					if(pageindex==1)
					{
						$("#info10").html("");
						$("#loading10").html("主人，没有找到您需要的数据");
					}else
					{
						$("#loading10").html("没有更多了");
					}
				}
         		pageindex = parseInt($("#weipanel10").attr("pageindex"))+1;
         		$("#weipanel10").attr("pageindex",pageindex);
			});
};

function Company_Search(o){
	var mobilelength=o.value.Trim().length;
	if(mobilelength==11){
	$(".weicompany").html('<div class="loading"><img src="img/loading.gif"/></div>');
	var data = {};
			data.act = "Find_CompanyByMobile";
			data.userid=userid;
			data.comid=comid;
			data.source=source;
			data.mobile=o.value.Trim();
			data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				$(".weicompany").html(CompanySearch_Data(json.data));
				Init_AgencyCompany(".weicompany");
			});
		}else{
			if(mobilelength==0)
			{
				$(".weicompany").html("");
			}else{
				$(".weicompany").html("您输入了"+mobilelength+"位手机号码");
			}
		}
};


function CompanySearch_Data(data){
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="weilist border-bottom" id="'+info.ID+'" style="text-align:left;"><div class="weicontext" style="width:100%;"><div class="weititle">'+info.Name+'</div><div class="weibottom"><div class="click" style="width:40%;">创建人：'+info.RealName+'</div><div class="createdate" style="width:60%;">注册时间：'+info.CreateDate.ToTime()+'</div></div></div><div class="charge"><button>去交费</button></div></div>');
	});
	if(data.length==0)
	{
		buffer.append('没有找到该手机号的公司');
	}
	return buffer.toString();
};

function Init_AgencyCompany(id){
	var isopen=true;
	mui(id).off("tap","a");
	mui(id).on("tap","a",function(){
		isopen=false;
		window.location.href="tel:"+$(this).html();
	});
	
	mui(id).off("tap",".weilist");
	mui(id).on("tap",".weilist",function(){
		if(!isopen){isopen=true;return;} //防止点击完电话号码后，弹出交费窗口
		obj10=$(this);
		OpenWindow2(3,"交费","确定",function(){
			var data = {};
			data.act = "Save_Charge";
			data.source=source;
			data.userid=userid;
			data.comid=obj10.attr("id");
			data.money=$("#txt_money").val().Trim();
			data.enddate=$("#txt_enddate").val().Trim();
			if(data.money=="")
			{
				plus.nativeUI.toast("请填写交费金额");
				return;
			}
			if(isNaN(data.money))
			{
				plus.nativeUI.toast("交费金额必须为数字");
				return;
			}
			if(data.enddate=="")
			{
				plus.nativeUI.toast("请选择到期时间");
				return;
			}
			data.sign=GetSign(data.act);
			plus.nativeUI.showWaiting( "正在交费..." );
			CloseWaiting(10000);
			mui.getJSON(admin_domain, data, function (json) {
				plus.nativeUI.closeWaiting();
				plus.nativeUI.toast(json.msg);
				if(json.state)
				{
					CloseWindow();
				}
			});
		});
		$("#contentlist3").html(Charge_Code());
		
		get("enddate_tool").addEventListener("tap",function(){
			obj=this;
			var t=new Date();
			if($(this).find("input").val()!="")
			{
				var year_month_day=$(this).find("input").val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj).find("input").val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
		});
	});
};

function Charge_Code(){
	var date=new Date();
	var year=date.getFullYear()+1;
	var month=date.getMonth()+1;
	var day=date.getDate(); 
	var buffer = new StringBuffer();
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">公司名称</div></div><div class="ctext"><input type="text" placeholder="" value="'+obj10.find(".weititle").html()+'" readonly="readonly"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">交费金额</div><div class="require">*</div></div><div class="ctext"><input id="txt_money" type="text" placeholder="填写交费金额" value="" /></div></div>');
	buffer.append('<div id="enddate_tool" class="crmsheet border-bottom"><div class="ctitle"><div class="cname">到期时间</div><div class="require">*</div></div><div class="ctext"><input id="txt_enddate" type="text" placeholder="选择到期时间" value="'+(year+"-"+month+"-"+day).ToDate()+'" readonly="readonly"/></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	return buffer.toString();
};


function Agency_Search_Code(){
	var buffer = new StringBuffer();
	buffer.append('<div class="searchline" style="margin-top:10px;"><div class="names">关键字</div><div class="option"><input id="keyword_search" type="text" placeholder="公司名称，创建人，手机号" value="" style="width:210px;"/></div></div>');
	buffer.append('<div class="searchline"><div class="names">注册时间</div><div class="option"><input id="begindate_search" type="text" class="date" placeholder="开始时间" readonly="readonly"/><input id="enddate_search" type="text" class="date" placeholder="结束时间" readonly="readonly"/></div></div>');
	buffer.append('<div class="searchline"><div class="names">到期时间</div><div class="option"><input id="begindate_expire" type="text" class="date" placeholder="开始时间" readonly="readonly"/><input id="enddate_expire" type="text" class="date" placeholder="结束时间" readonly="readonly"/></div></div>');
	return buffer.toString();
};

function Init_Agency_Search(){
	mui(".option").on("tap",".date",function(){
		obj=this;
			var t=new Date();
			if($(this).val()!="")
			{
				var year_month_day=$(this).val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj).val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
			},{date:t});
	});
	
};